#include "stdafx.h"
#include "BBViewer.h"
#include "MainFrm.h"
#include "UIMain.h"

#include "../_inc_util/UtilPath.h"
#include "../_inc_util/UtilSound.h"
#include "../_inc_util/UtilRender.h"
#include "../_inc_util/UtilLog.h"
// 
#include "DlgReport.h"
#include "DlgOpenDrive.h"
// 
#include "DlgFileDelete.h"
#include "DlgColor.h"
#include "../_inc_set/def_config.h"

long CUIMain::OnCtlMessage(UINT wParam, LONG lParam)
{
	int nControlID		= wParam;			// Control ID
	int nControlValue	= (int)lParam;		// Control Value

	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biFileOpen_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		OpenRecFile();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biMenuOpen_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		OpenRecFile();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biDelete_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		DeleteSelectedFiles();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biMenuDel_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		DeleteSelectedFiles();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biExit_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		BOOL bRestart = (BOOL)lParam;
		Exit(bRestart);
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biMin_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		g_pMainFrame->ShowWindow(SW_MINIMIZE);
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biFull_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		FullScreen();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biRestoreFF_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		FullScreen();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biMax_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		MaxScreen();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biRestoreFM_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		MaxScreen();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biSetup_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		OnSetup();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biPrint_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		PrintReport();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biBackup_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		OnBackup();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biViewer_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		OnViewerSetupEx();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biTabTime_unchecked);
	if (lpBmpInfo->uImageID == nControlID)
	{
		OnTabTime();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biTabEvent_unchecked);
	if (lpBmpInfo->uImageID == nControlID)
	{
		OnTabEvent();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biGpsNormal_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		OnShowGPSPanel();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biGpsHide_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		OnShowGPSPanel();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biGpsLarge_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		OnShowGPSPanel();
		return 0;
	}


	switch (nControlID)
	{
	case DO_RECOVERY_INDEX_TIME:
		DoRecoveryIndexTime(nControlValue);
		break;
	case ASK_RECOVERY_INDEX_TIME:
		{
			BOOL *bRecovery = (BOOL *)lParam;
			*bRecovery = AskRecoveryIndexTime(0);
		}
		break;

	case DO_RECOVERY_INDEX_EVENT:
		DoRecoveryIndexEvent(nControlValue);
		break;
	case ASK_RECOVERY_INDEX_EVENT:
		{
			BOOL *bRecovery = (BOOL *)lParam;
			*bRecovery = AskRecoveryIndexEvent(0);
		}
		break;
	case DO_RECOVERY_FILEOPEN:
		DoRecoveryFileOpen();
		break;
	case WM_USER_DISPLAYCHANGE:
		{
			if (!m_bDisplayChange)
				break;
			theApp.SetDlgSize();
			CRect rtMonitor, rtWork;

			if (m_bFull)
			{
				if (m_bMaximize)
				{
					GetMonitorRect(g_pMainFrame->GetSafeHwnd(), &m_rtRestoreFull, TRUE);
					m_rtRestoreMax = CRect(0, 0, theApp.dwWinWidth, theApp.dwWinHeight);
				}
				else
				{
					m_rtRestoreFull = CRect(0, 0, theApp.dwWinWidth, theApp.dwWinHeight);
				}

				GetMonitorRect(g_pMainFrame->GetSafeHwnd(), &rtMonitor);
				g_pMainFrame->MoveWindow(&rtMonitor);
			}
			else if (m_bMaximize)
			{
				GetMonitorRect(g_pMainFrame->GetSafeHwnd(), &rtWork, TRUE);
				m_rtRestoreMax = CRect(0, 0, theApp.dwWinWidth, theApp.dwWinHeight);
				g_pMainFrame->MoveWindow(&rtWork);
			}
			else
			{
				g_pMainFrame->MoveWindow(0, 0, theApp.dwWinWidth, theApp.dwWinHeight);

			}
			Refresh();
			m_bDisplayChange = FALSE;
		}
		break;
	}

	return  0;
}

long CUIMain::OnPlbMessage(UINT wParam, LONG lParam)
{
	int nControlID		= wParam;			// Control ID
	int nControlValue	= lParam;

	DWORD dwValue = (DWORD)lParam;
	LPST_SET_COLOR lpSetColor = (LPST_SET_COLOR)lParam;

	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biPlayForward_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		PlayForward();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biPause_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		Pause();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biPlayBackward_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		PlayBackward();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biStop_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		PlayStop();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biStepForward_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		StepForward();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biStepBackward_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		StepBackwardKeyframe();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biStepForward_k_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
 		StepForwardKeyframe();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biStepBackward_k_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		StepBackwardKeyframe();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biNextFile_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		NextFile();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biPrevFile_normal);
	if (lpBmpInfo->uImageID == nControlID)
	{
		PrevFile();
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biPBSpeed_bk);
	if (lpBmpInfo->uImageID == nControlID)
	{
		int nPBSpeed = nControlValue;
		m_pIntrPlayEx->SetPBSpeed(nPBSpeed);
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biVol_bk);
	if (lpBmpInfo->uImageID == nControlID)
	{
		SetVolume(nControlValue);
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biOptNormal_unchecked);
	if (lpBmpInfo->uImageID == nControlID)
	{
		SearchEvent(nControlValue);
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biOptEvent_unchecked);
	if (lpBmpInfo->uImageID == nControlID)
	{
		SearchEvent(nControlValue);
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biOptParking_unchecked);
	if (lpBmpInfo->uImageID == nControlID)
	{
		SearchEvent(nControlValue);
		return 0;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biOptManual_unchecked);
	if (lpBmpInfo->uImageID == nControlID)
	{
		SearchEvent(nControlValue);
		return 0;
	}


	switch (nControlID)
	{
	case IDC_LIST_HOUR:
	case IDC_LIST_DATE:
		{
			PlayStop();			
		}		
		break;
	case IDC_LIST_TIME:
		{
			if (nControlValue<0)
				break;

			if (m_bPlayOn)
			{
				m_bPlayOn = FALSE;
				m_bLoadUserData = FALSE;
				Sleep(100);
				m_pIntrPlayEx->Stop();
				Sleep(100);
				m_pIntrPlayEx->ClearData();
			}
			else
			{
				SetZoomDefault();
			}

			int nPlayListNo = m_WndListTime.GetRecIndexNumberFromListNumber(nControlValue);
			m_pIntrPlayEx->SetSearchType(m_nSearchType);
			m_bLoadUserData = TRUE;
			m_pIntrPlayEx->PlayList(nPlayListNo, TRUE);

			m_WndGSensorGraph.ClearGSensor();
			m_bPlayOn = TRUE;
			m_WndGps.SetPlayStatus(m_bPlayOn);
			m_WndPlayer.SetPlayStatus(m_bPlayOn);
			m_WndTimeBar.SetPlayStatus(m_bPlayOn);
			m_WndMenuButton.SetBackupButtonStatus(m_bPlayOn);
			m_WndMenuButton.SetPrintButtonStatus(m_bPlayOn);			
		}
		break;
	case IDC_LIST_EVENT:
		{
			if (nControlValue<0)
				break;

			if (m_bPlayOn)
			{
				m_bPlayOn = FALSE;
				m_bLoadUserData = FALSE;
				Sleep(100);
				m_pIntrPlayEx->Stop();
				Sleep(100);
				m_pIntrPlayEx->ClearData();
			}
			else
			{
				SetZoomDefault();
			}
			m_pIntrPlayEx->SetSearchType(m_nSearchType);
			m_bLoadUserData = TRUE;
			m_pIntrPlayEx->PlayList(nControlValue, TRUE);

			m_WndGSensorGraph.ClearGSensor();
			m_bPlayOn = TRUE;
			m_WndGps.SetPlayStatus(m_bPlayOn);
			m_WndPlayer.SetPlayStatus(m_bPlayOn);
			m_WndTimeBar.SetPlayStatus(m_bPlayOn);
			m_WndMenuButton.SetBackupButtonStatus(m_bPlayOn);
			m_WndMenuButton.SetPrintButtonStatus(m_bPlayOn);			
		}
		break;
	case IDC_SCREEN_POPUP:
		{
			if (nControlValue == 0)
				m_pIntrPlayEx->InitPopupScreen();
			else
				m_pIntrPlayEx->ClosePopup();			
		}
		break;
	case WM_TIME_LIST_CHECK_STATUS:
		{
			if (m_nSearchType == search_time)
			{
				m_WndMenuButton.SetBackupButtonStatus(nControlValue||m_bPlayOn);
				m_WndMenuButton.SetDeleteButtonStatus(nControlValue);
				m_btnDelete.EnableWindow(nControlValue);
			}
		}
		break;
	case WM_EVENT_LIST_CHECK_STATUS:
		{
			if (m_nSearchType == search_event)
			{
				m_WndMenuButton.SetBackupButtonStatus(nControlValue||m_bPlayOn);
				m_WndMenuButton.SetDeleteButtonStatus(nControlValue);
				m_btnDelete.EnableWindow(nControlValue);
			}
		}
		break;
	case WM_TIME_BAR_HOLD:
		{
			if (m_pIntrPlayEx->HoldCurrentImage())
				return 0;

			TRACE(_T("Filed to hold Image\n"));
			return 1;
		}
		break;
	case WM_TIME_BAR_RELEASE:
		{
			m_pIntrPlayEx->ReleaseCurrentImage();
		}
		break;
	case WM_TIME_BAR_SEARCH:
		{
			m_pIntrPlayEx->GotoTimeSearch(nControlValue);
		}
		break;
	case WM_TIME_BAR_SEARCH_BEGIN:
		{
			m_pIntrPlayEx->GotoTimeSearchBegin(nControlValue);
		}
		break;
	case IDC_SLD_BRIGHTNESS:
		{
			m_pIntrPlayEx->SetBrightness(lpSetColor->nChannel, lpSetColor->nValue);
		}
		break;
	case IDC_SLD_CONTRAST:
		{
			m_pIntrPlayEx->SetContrast(lpSetColor->nChannel, lpSetColor->nValue);
		}
		break;
	case IDC_SLD_SATURATION:
		{
			m_pIntrPlayEx->SetSaturation(lpSetColor->nChannel, lpSetColor->nValue);
		}
		break;
	case IDC_SLD_SHARPENESS :
		{
			m_pIntrPlayEx->SetSharpeness(lpSetColor->nChannel, lpSetColor->nValue);
		}
		break;
	case IDC_SLD_BLUR :
		{
			m_pIntrPlayEx->SetBlur(lpSetColor->nChannel, lpSetColor->nValue);
		}
		break;
	default :
		break;
	}

	return  0;
}

LRESULT CUIMain::OnRefreshSkin(WPARAM wParam, LPARAM lParam)
{
	return 0;
}

void CUIMain::RefreshSkin()
{
	if (m_bPlayOn)
		PlayStop();

	Exit(TRUE);

}


//////////////////////////////////////////////////////////////////////////
void CUIMain::Exit(BOOL bRestart)
{
	//////////////////////////////////////////////////////////////////////////
	//2012.09.05
	//changes:
	//1. CloseIndex after stops playing
	//2. call "g_pMainFrame->PostMessage(WM_CLOSE);" at the end of function just before restart program if bRestart = TRUE
	//////////////////////////////////////////////////////////////////////////

	m_bInit = FALSE;

	//1. Stop playing if playing is in progress
	if (m_pIntrPlayEx->GetPlayerState() != pst_stopped)
	{
		m_bLoadUserData = FALSE;
		Sleep(100);
		m_pIntrPlayEx->Stop();
		Sleep(100);
		m_pIntrPlayEx->ClearData();
	}
	m_pIntrPlayEx->Release();

	CloseIndex();

	m_ExtScreen3.ShowWindow(SW_HIDE);
	m_ExtScreen3.SetShowStatus(FALSE);
	Sleep(10);
	m_ExtScreen3.DestroyWindow();

	KillTimer(TIMER_ID_RESETDISPLAY);

	int i;
	for (i=0;i<MAX_SCREEN_COUNT;i++)
	{
		if (m_pWndDisplay[i])
			delete m_pWndDisplay[i];
		m_pWndDisplay[i] = NULL;
	}

	DeleteDxSound();

	if (m_lpstFileNameFormat)
		delete m_lpstFileNameFormat;
	m_lpstFileNameFormat = NULL;

	ExitSharedMemory();
	delete m_pDlgColor;

	Release();

	CUtilLog::WriteLog(theApp.GetLogPath(), _T("Close BBViewer.exe"));

	g_pMainFrame->PostMessage(WM_CLOSE);

	if (bRestart)
	{
		TCHAR tzFileName[MAX_PATH];
		GetModuleFileName(GetModuleHandle(NULL), tzFileName, MAX_PATH);

		STARTUPINFO startInfo;
		PROCESS_INFORMATION processInfo;
		ZeroMemory(&startInfo, sizeof(startInfo));
		startInfo.cb = sizeof(startInfo);
		startInfo.wShowWindow = SW_SHOWNORMAL;

		CreateProcess(tzFileName, NULL, NULL, NULL, FALSE, 
			CREATE_BREAKAWAY_FROM_JOB|CREATE_NEW_PROCESS_GROUP, 
			NULL, NULL, &startInfo, &processInfo);

		CloseHandle(startInfo.hStdInput);
		CloseHandle(startInfo.hStdOutput);
		CloseHandle(startInfo.hStdError);

		CloseHandle(processInfo.hProcess);
		CloseHandle(processInfo.hThread);

	}
}

void CUIMain::OpenRecFile()
{
	CUtilPath util;
	CUtilPath *pUtilPath;
	static TCHAR tzPath[MAX_PATH] = {0, };
	TCHAR tzTitle[MAX_PATH] = {0};

	CDlgOpenDrive *pDlgOpen = NULL;
	pDlgOpen = new CDlgOpenDrive();

	int nCount;
	nCount = theApp.GetOptionPathCount();
	if (nCount == 0)
		nCount = 1;

	if (pDlgOpen->DoModal() == IDOK)
	{
		if (pDlgOpen->GetSelectedList() == -2)
		{
			delete pDlgOpen;
			goto _SELECT_FOLDER;
		}
		else if (pDlgOpen->GetSelectedList() == -1)
		{
			delete pDlgOpen;
			return;
		}
		else
		{
			wsprintf(tzPath, pDlgOpen->GetFolder());
			delete pDlgOpen;
			goto _SELECTED_FOLDER;
		}
	}
	else
	{
		delete pDlgOpen;
		return;
	}

_SELECT_FOLDER:
	wsprintf(tzTitle, _T("%s"), m_sarrMessage.GetAt(Message_2));
	pUtilPath = new CUtilPath();

	theApp.GetLatestPath(tzPath, MAX_PATH);

	if (pUtilPath->SelectFolder(m_hWnd, tzPath, tzTitle, tzPath, FALSE))
	{
		delete pUtilPath;
_SELECTED_FOLDER:
		RedrawWindow();

		m_pStatusMgr->SetConfigPath(tzPath);

		m_pIntrPlayEx->SetDbPath(tzPath);
		if (m_bPlayOn)
		{
			m_bLoadUserData = FALSE;
			Sleep(100);
			m_pIntrPlayEx->Stop();
			Sleep(100);
			m_pIntrPlayEx->ClearData();
		}

		if (m_pStatusMgr->OpenKConfig())
		{

			m_WndSpeedmeter.SetViewType(m_pStatusMgr->GetSpeedometer());
			m_WndGps.SetSpeedUnit(m_pStatusMgr->GetSpeedometer());

			VIEWER_SETTING stViewerSet;
			stViewerSet = m_dlgViewerSetup.GetViewerSet();

			if (stViewerSet.bAutoScreenMode)
			{
				TCHAR *ptzModelPath;
				CDevConfig cDevCfg;
				BOOL bRefresh = FALSE;

				ptzModelPath = m_pStatusMgr->GetModelPath();

				cDevCfg.Load(ptzModelPath);
				if (cDevCfg.enable(RearCamera))
				{
					//screen mode 1
					if (stViewerSet.dwViewerMode != ViewerMode_2)
						bRefresh = TRUE;
					stViewerSet.dwViewerMode = ViewerMode_2;
				}
				else
				{
					//screen mode 2
					if (stViewerSet.dwViewerMode != ViewerMode_1)
						bRefresh = TRUE;
					stViewerSet.dwViewerMode = ViewerMode_1;
				}
				m_dlgViewerSetup.LoadViewerSet(&stViewerSet);

			}

		}
		Refresh();

		//////////////////////////////////////////////////////////////////////////
		//ClearScreen
		//////////////////////////////////////////////////////////////////////////
		LoadCursorWaiting();

		CFileOpenProgress dlgOpen;

		dlgOpen.SetMode(DLG_OPEN_MODE_1);
		dlgOpen.SetReplayEx(m_pIntrPlayEx);
		dlgOpen.SetCallback(CallbackReplay, this);
		dlgOpen.SetFileFormatList(m_lpstFileNameFormat, nCount);

		dlgOpen.DoModal();

		dlgOpen.SetMode(DLG_OPEN_MODE_0);
		dlgOpen.SetReplayEx(NULL);

		m_bFileOpen = dlgOpen.GetFileOpenStatus();

		if (!m_bFileOpen)
		{
			LoadCursorArrow();
			AfxMessageBox(m_sarrMessage.GetAt(Message_4));
			return;
		}
		m_nCurrentFileFormat = dlgOpen.GetCurrentFileFormat();
		LoadCursorArrow();
		m_WndGSensorGraph.ClearGSensor();

		theApp.WriteLatestPath(tzPath, MAX_PATH);

		if (m_bFull)
		{
			LPST_BMP_INFO lpBmpInfo = theApp.BmpInfo.GetAt(biPlayForward_normal);
			this->PostMessage(WM_PLB_MESSAGE, lpBmpInfo->uImageID, 0);
		}
	}
	else
	{
		delete pUtilPath;
	}
	RedrawWindow();
	m_WrapMap.ShowWindow(SW_HIDE);
	m_WrapMap.ShowWindow(SW_SHOW);
}


void CUIMain::DeleteSelectedFiles()
{
	int nCheck = -1;

	if (m_nSearchType == search_time)
		nCheck = m_WndListTime.GetBackupIndexStart();
	else
		nCheck = m_WndListEvent.GetBackupIndexStart();

	if (nCheck<0)
	{
		AfxMessageBox(m_sarrMessage.GetAt(Message_12));
		return;
	}

	if (AfxMessageBox(m_sarrMessage.GetAt(Message_5), MB_OKCANCEL|MB_ICONEXCLAMATION) == IDCANCEL)
		return;

	m_bLoadUserData = FALSE;
	Sleep(100);
	m_pIntrPlayEx->Stop();
	Sleep(100);
	m_pIntrPlayEx->ClearData();

	StoppedUi();

	CDlgFileDelete *pDlg;
	pDlg = new CDlgFileDelete(this);

	pDlg->SetSearchType(m_nSearchType);
	pDlg->DoModal();

	int nIndex = pDlg->GetIndex();

	delete pDlg;

	CFileOpenProgress dlgOpen;
	int nCount;
	nCount = theApp.GetOptionPathCount();
	if (nCount == 0)
		nCount = 1;

	dlgOpen.SetMode(DLG_OPEN_MODE_1);
	dlgOpen.SetReplayEx(m_pIntrPlayEx);
	dlgOpen.SetCallback(CallbackReplay, this);
	dlgOpen.SetFileFormatList(m_lpstFileNameFormat, nCount);

	dlgOpen.DoModal();

	dlgOpen.SetMode(DLG_OPEN_MODE_0);
	dlgOpen.SetReplayEx(NULL);

	m_bFileOpen = dlgOpen.GetFileOpenStatus();

	m_WndGSensorGraph.ClearGSensor();
	if (!m_bFileOpen)
	{
		m_WndListTime.ResetList();
		m_WndListEvent.ResetList();
		m_pListMgr->ResetRecList();
		m_WndListTime.LoadRecList();
		m_WndListEvent.LoadRecList();
		ShowLogo();
		return;
	}

	if (nIndex>=0)
	{
		if (m_nSearchType == search_time)
			m_WndListTime.SetRecIndex(nIndex);
		else
			m_WndListEvent.SetRecIndex(nIndex);
	}

	ShowLogo();
}

void CUIMain::MaxScreen()
{
	int i;
	CRect rtWnd;
	CRect rtWorkArea;

	if (m_bFull)
		return;

	m_bMaximize = !m_bMaximize;
	m_WndSystemMenu.SetCheckMaximize(m_bMaximize);
	for (i=0;i<MAX_SCREEN_COUNT;i++)
	{
		m_pWndDisplay[i]->SetAspectRatioMode(m_dwRatioMode, m_bMaximize);
		m_pWndDisplay[i]->UpdateScreen2(!m_bPlayOn);
	}
	if (m_bMaximize == TRUE)
	{
		g_pMainFrame->GetWindowRect(&m_rtRestoreMax);
		GetMonitorRect(g_pMainFrame->GetSafeHwnd(), &rtWnd, TRUE);
		g_pMainFrame->MoveWindow(&rtWnd);
	}
	else
	{
		g_pMainFrame->MoveWindow(m_rtRestoreMax);
	}
	if (m_bPlayOn)
		m_pIntrPlayEx->ShowCurrentImage();
}

void CUIMain::FullScreen()
{
	static BOOL bFullScreen;
	CRect rtWnd;
	int i;

	m_bFull = !m_bFull;
	m_WndSystemMenu.SetCheckFull(m_bFull);
	GetMonitorRect(g_pMainFrame->GetSafeHwnd(), &rtWnd);

	if (m_bFull)
	{
		bFullScreen = m_bFullScreen;
		for (i=0;i<MAX_CHANNEL;i++)
			m_pWndDisplay[i]->SetDisplayStatue(FALSE);

		m_ExtScreen3.SetAspectRatio(m_dwRatioMode);
		m_ExtScreen3.MoveWindow(&rtWnd);
		m_ExtScreen3.ShowWindow(SW_SHOW);
		m_nPopupCamera = m_bSwapScreen ? 1 : 0;

		if (m_bFullScreen)
		{
			m_nPopupCamera = m_nFullScrChannel;
		}
		else
		{
			m_bFullScreen = TRUE;
			m_nFullScrChannel = m_nPopupCamera;
		}
		EnlargeView(m_bFullScreen);

		CRect rtZoom, rtDst;
		CPoint ptZoom;
		int nZoom;

		m_pWndDisplay[m_nPopupCamera]->GetDispZoomStatus(nZoom, ptZoom, rtZoom, rtDst);
		m_ExtScreen3.SetDispZoomStatus(nZoom, ptZoom, rtZoom, rtDst);

		m_ExtScreen3.SetDisplayChannel(m_nPopupCamera);
		m_ExtScreen3.SetVideoReverse();
		m_pIntrPlayEx->PopupRequest(m_nPopupCamera);
		g_pMainFrame->GetWindowRect(&m_rtRestoreFull);
		g_pMainFrame->MoveWindow(&rtWnd);

	}
	else
	{
		//////////////////////////////////////////////////////////////////////////
		//
		CRect rtZoom, rtDst;
		CPoint ptZoom;
		int nZoom;


		m_ExtScreen3.GetDispZoomStatus(nZoom, ptZoom, rtZoom, rtDst);
		m_pWndDisplay[m_nPopupCamera]->SetDispZoomStatus(nZoom, ptZoom, rtZoom, rtDst);

		for (i=0;i<MAX_CHANNEL;i++)
			m_pWndDisplay[i]->SetDisplayStatue(TRUE);

		m_nPopupCamera = -1;
		m_pIntrPlayEx->PopupRequest(m_nPopupCamera);
		g_pMainFrame->MoveWindow(m_rtRestoreFull);

		if (bFullScreen != m_bFullScreen)
		{
			m_bFullScreen = bFullScreen;
			EnlargeView(m_bFullScreen);
			m_nFullScrChannel = 0;
		}

		m_ExtScreen3.ShowWindow(SW_HIDE);
	}
}

void CUIMain::OnSetup()
{
	LPST_SHARED_INFO lpInfo;

	lpInfo = (LPST_SHARED_INFO)m_pSharedMemSetup;
	lpInfo->dwFlag = SETUP_FLAG_REQUEST_TO_OPEN;
	ZeroMemory(lpInfo->tzPath, sizeof(lpInfo->tzPath));

	BOOL bOpenConfig = m_pStatusMgr->IsOpenConfig();
	if (bOpenConfig)
	{
		wsprintf(lpInfo->tzPath, _T("%s\\"), m_pStatusMgr->GetSelectPath());
	}

	TCHAR tzFileName[MAX_PATH];
	ZeroMemory(tzFileName, sizeof(tzFileName));
	wsprintf(tzFileName, _T("%s\\BBSetup.exe"), theApp.m_strModulePath);


	STARTUPINFO startInfo;
	PROCESS_INFORMATION processInfo;
	ZeroMemory(&startInfo, sizeof(startInfo));
	startInfo.cb = sizeof(startInfo);
	startInfo.wShowWindow = SW_SHOWNORMAL;

	CreateProcess(tzFileName, NULL, NULL, NULL, FALSE, 
		CREATE_BREAKAWAY_FROM_JOB|CREATE_NEW_PROCESS_GROUP, 
		NULL, NULL, &startInfo, &processInfo);

	CloseHandle(startInfo.hStdInput);
	CloseHandle(startInfo.hStdOutput);
	CloseHandle(startInfo.hStdError);

	CloseHandle(processInfo.hProcess);
	CloseHandle(processInfo.hThread);
	return;
}

void CUIMain::OnApplySetup()
{
	//0. open config file first
	LPST_SHARED_INFO lpInfo = (LPST_SHARED_INFO)m_pSharedMemSetup;
	if (lpInfo->dwFlag & SETUP_FLAG_RESPONSE)
	{
		lpInfo->dwFlag &= ~ SETUP_FLAG_RESPONSE;
		m_pStatusMgr->SetConfigPath(lpInfo->tzPath);
		m_pStatusMgr->OpenKConfig();

		m_WndSpeedmeter.SetViewType(m_pStatusMgr->GetSpeedometer());
		m_WndGps.SetSpeedUnit(m_pStatusMgr->GetSpeedometer());
	}

	//2. language
	if (lpInfo->dwFlag & SETUP_FLAG_LANGUAGE)
	{
		//refresh language
		lpInfo->dwFlag &= ~ SETUP_FLAG_LANGUAGE;

		theApp.GetLanguageSupport();

		LoadText();

		m_ToolTipCtrl.DelTool(&m_btnOpen);
		m_ToolTipCtrl.DelTool(&m_btnDelete);

		m_ToolTipCtrl.AddTool(&m_btnOpen, m_sarrText.GetAt(0));
		m_ToolTipCtrl.AddTool(&m_btnDelete,m_sarrText.GetAt(1));

		m_ExtScreen3.RefreshLang();
		m_pListMgr->RefreshLang();

		m_WndGps.RefreshLang();
		m_WndGSensor.RefreshLang();
		m_WndMenuButton.RefreshLang();
		m_WndPlayer.RefreshLang();
		m_WndSystemMenu.RefreshLang();
		m_WndTab.RefreshLang();

		m_WndSearchOpt.RefreshLang();
		m_WndListTime.RefreshLang();
		m_WndListEvent.RefreshLang();
	}

	//3. skin
	if (lpInfo->dwFlag & SETUP_FLAG_SKIN)
	{
		//refresh skin
		lpInfo->dwFlag &= ~ SETUP_FLAG_SKIN;

		this->PostMessage(WM_REFRESH_SKIN, 0, 0);
	}
}

void CUIMain::PrintReport()
{
	int i;
	if (m_pIntrPlayEx->GetPlayerState() == pst_stopped)
		return;

	if (m_pIntrPlayEx->GetPlayerState() != pst_pause)
	{
		m_WndPlayer.SetPlayStatus(FALSE);
		m_pIntrPlayEx->Pause();
		Sleep(100);
	}

	CDlgReport dlg;
	REPORT_ITEM item;
	VIEWER_SETTING stViewerSet = m_dlgViewerSetup.GetViewerSet();

	dlg.SetViewerSetup(&m_dlgViewerSetup);
	item.dwTime = (DWORD)m_stImageInfo.tPlayTime;
	item.fEGps = m_stImageInfo.gps_longitude/100;
	item.fNGps = m_stImageInfo.gps_latitude/100;
	item.fXSensor = m_stImageInfo.gsensor_x;
	item.fYSensor = m_stImageInfo.gsensor_y;
	item.fZSensor = m_stImageInfo.gsensor_z;
	item.fSpeed = m_stImageInfo.gps_speed;
	item.nSpeedMode = m_pStatusMgr->GetSpeedometer();

	BYTE *pVideoBuffer;
	int nWidth;
	int nHeight;
	int nOutputFormat;
	for (i=0;i<(int)stViewerSet.dwViewerMode;i++)
	{
		pVideoBuffer = m_pIntrPlayEx->GetLastVideoImage(i, &nWidth, &nHeight, &nOutputFormat);
		item.vidStream[i].dwWidth = nWidth;
		item.vidStream[i].dwHeight = nHeight;
		if (pVideoBuffer)
		{
			item.vidStream[i].pStream = m_pWndDisplay[i]->GetPrintData(0, pVideoBuffer, nWidth, nHeight, nOutputFormat);
		}
		else
		{
			item.vidStream[i].pStream = NULL;
		}
	}

	for (;i<MAX_CHANNEL;i++)
	{
		item.vidStream[i].pStream = NULL;
		item.vidStream[i].dwWidth = 640;
		item.vidStream[i].dwHeight = 360;
	}
	dlg.SetReportItem(&item);

	dlg.DoModal();

}

void CUIMain::OnBackup()
{
	int i;
	int nStat = -1;
	if (m_nSearchType == search_time)
	{
		if (m_WndListTime.GetBackupIndexStart()>=0)
			nStat = 1;
	}
	else
	{
		if (m_WndListEvent.GetBackupIndexStart()>=0)
			nStat = 1;
	}

	if (nStat<0)
	{
		if (m_pIntrPlayEx->GetPlayerState() != pst_stopped)
			nStat = 0;
	}

	if (nStat<0)
		return;

	VIEWER_SETTING stViewerSet = m_dlgViewerSetup.GetViewerSet();
	m_DlgBackup.SetAVIFileNameFormat(m_lpstFileNameFormat[m_nCurrentFileFormat]);
	m_DlgBackup.SetBackupPath(stViewerSet.tzSnapshotPath);
	m_DlgBackup.SetRecMode(m_pIntrPlayEx->GetRecordMode());

	if (nStat == 0)
	{
		if (m_pIntrPlayEx->GetPlayerState() != pst_pause)
		{
			m_WndPlayer.SetPlayStatus(FALSE);
			m_pIntrPlayEx->Pause();
			Sleep(100);
		}

		m_DlgBackup.SetBackupStartIndex(m_pIntrPlayEx->GetCurrentIndex(0), (SEARCH_TYPE)m_pIntrPlayEx->GetSearchType()); 
	}
	else
	{
		m_DlgBackup.SetBackupStartIndex(-1, m_nSearchType); 
	}

	for (i=0;i<MAX_CHANNEL;i++)
	{
		int nWidth, nHeight, nOutputFormat;
		BYTE *pVidFrame;
		pVidFrame = m_pIntrPlayEx->GetLastVideoImage(i, &nWidth, &nHeight, &nOutputFormat);
		if (pVidFrame)
		{
			m_DlgBackup.SetCurrentImage(i, pVidFrame, nWidth, nHeight, nOutputFormat, m_stImageInfo.tPlayTime);
		}
	}

	m_DlgBackup.DoModal();

}

void CUIMain::OnViewerSetupEx()
{
	m_dlgViewerSetup.CenterWindow(this);
	m_dlgViewerSetup.ShowWindow(SW_SHOW);
}

LRESULT CUIMain::OnApplyViewerSetup(WPARAM wParam, LPARAM lParam)
{
	if (m_dlgViewerSetup.ChangeStatusRestart())
	{
		LPST_SHARED_INFO lpInfo = (LPST_SHARED_INFO)m_pSharedMemSetup;

		lpInfo->dwFlag &= 0xFF;

		lpInfo->dwFlag |= SETUP_FLAG_SKIN_SETUP;
		if (m_dlgViewerSetup.ChangeStatusPlayStop() & VIEWER_FLAG_UI_LANGUAGE)
			lpInfo->dwFlag |= SETUP_FLAG_LANG_SETUP;

		m_pEtToSetup->SetEvent();

		Exit(TRUE);
	}
	else 
	{
		BOOL bRefresh = FALSE;

		VIEWER_SETTING stViewerSet = m_dlgViewerSetup.GetViewerSet();

		if (m_dlgViewerSetup.ChangeStatusPlayStop())
		{
			PlayStop();
			if (m_dlgViewerSetup.ChangeStatusPlayStop() & 
				(VIEWER_FLAG_DISPLAY_MODE_AUTO|VIEWER_FLAG_DISPLAY_MODE))
			{
				bRefresh = TRUE;
				if (m_pStatusMgr->OpenKConfig())
				{
					TCHAR *ptzModelPath;
					CDevConfig cDevCfg;
					BOOL bRefresh = FALSE;

					ptzModelPath = m_pStatusMgr->GetModelPath();

					cDevCfg.Load(ptzModelPath);
					if (cDevCfg.enable(RearCamera))
					{
						//screen mode 1
						if (stViewerSet.dwViewerMode != ViewerMode_2)
							bRefresh = TRUE;
						stViewerSet.dwViewerMode = ViewerMode_2;
					}
					else
					{
						//screen mode 2
						if (stViewerSet.dwViewerMode != ViewerMode_1)
							bRefresh = TRUE;
						stViewerSet.dwViewerMode = ViewerMode_1;
					}

					m_dlgViewerSetup.LoadViewerSet(&stViewerSet);
				}
				
			}

			if (m_dlgViewerSetup.ChangeStatusPlayStop() & VIEWER_FLAG_CTRL_VIDEO_SYNC)
			{
				m_pIntrPlayEx->SetRearCameraDelay((int)stViewerSet.wVideoSync);
			}

			if (m_dlgViewerSetup.ChangeStatusPlayStop() & VIEWER_FLAG_UI_GSENSOR)
			{
				bRefresh = TRUE;
				m_WndGSensorGraph.SetGSensorGraphType(stViewerSet.bGSensorGraph);
				BOOL bReadInAdvance;
				if (stViewerSet.bGSensorGraph == GSENSOR_GRAPH_FILE_BASED)
					bReadInAdvance = TRUE;
				else
					bReadInAdvance = FALSE;
#ifdef _EX_HEADER_INFO
				m_pIntrPlayEx->SetUserFormatStatus(TRUE, sizeof(PICTURE_HEADER_EX), bReadInAdvance);
#else
				m_pIntrPlayEx->SetUserFormatStatus(FALSE, sizeof(PICTURE_HEADER_EX), bReadInAdvance);
#endif
			}

			if (m_dlgViewerSetup.ChangeStatusPlayStop() & VIEWER_FLAG_UI_LANGUAGE)
			{
				LPST_SHARED_INFO lpInfo = (LPST_SHARED_INFO)m_pSharedMemSetup;

				lpInfo->dwFlag &= 0xFF;
				lpInfo->dwFlag |= SETUP_FLAG_LANG_SETUP;

				m_pEtToSetup->SetEvent();

				theApp.GetLanguageSupport();

				LoadText();

				m_ToolTipCtrl.DelTool(&m_btnOpen);
				m_ToolTipCtrl.DelTool(&m_btnDelete);

				m_ToolTipCtrl.AddTool(&m_btnOpen, m_sarrText.GetAt(0));
				m_ToolTipCtrl.AddTool(&m_btnDelete,m_sarrText.GetAt(1));

				m_ExtScreen3.RefreshLang();
				m_pListMgr->RefreshLang();

				m_WndGps.RefreshLang();
				m_WndGSensor.RefreshLang();
				m_WndMenuButton.RefreshLang();
				m_WndPlayer.RefreshLang();
				m_WndSystemMenu.RefreshLang();
				m_WndTab.RefreshLang();

				m_WndSearchOpt.RefreshLang();
				m_WndListTime.RefreshLang();
				m_WndListEvent.RefreshLang();

				m_dlgViewerSetup.RefreshLang();

			}

			if (bRefresh)
			{
				Refresh();
			}
		}

		if (m_dlgViewerSetup.ChangeStatusNormal() & 
			(VIEWER_FLAG_DISPLAY_PIP_SHOW|VIEWER_FLAG_DISPLAY_PIP_POS|VIEWER_FLAG_DISPLAY_PIP_KEY))
		{
			SetZoomPIP();
		}

		if (m_dlgViewerSetup.ChangeStatusNormal() & VIEWER_FLAG_DISPLAY_FLIP)
		{
			SetVideoReverse();
			m_ExtScreen3.SetVideoReverse();
		}

		if (m_dlgViewerSetup.ChangeStatusNormal() & VIEWER_FLAG_UI_MAP)
		{
			SetMapConnection(stViewerSet.bShowMap);
		}

		if (m_dlgViewerSetup.ChangeStatusNormal() & VIEWER_FLAG_CTRL_PBSPEED_OPT)
		{
			m_pIntrPlayEx->SetPBSpeedOption(stViewerSet.bPBSpeed);
		}

		if (m_dlgViewerSetup.ChangeStatusNormal() & VIEWER_FLAG_RECOVERY)
		{
			m_pIntrPlayEx->EnableRecovery((BOOL)stViewerSet.bRecovery);
		}


	}


	return 0;
}

void CUIMain::OnTabTime()
{
	m_nTabIndex = 0;
	m_WndTab.SetTab(m_nTabIndex);				
	m_WndListEvent.ShowWindow(SW_HIDE);
	m_WndListTime.ShowWindow(SW_SHOW);

	m_WndSearchOpt.SetOptTime();

	m_nSearchType = search_time;
	m_WndMenuButton.SetBackupButtonStatus(m_WndListTime.GetCheckStatus()||m_bPlayOn);
	m_WndMenuButton.SetDeleteButtonStatus(m_WndListTime.GetCheckStatus());
	m_btnDelete.EnableWindow(m_WndListTime.GetCheckStatus());
}

void CUIMain::OnTabEvent()
{
	m_nTabIndex = 1;
	m_WndTab.SetTab(m_nTabIndex);				
	m_WndListTime.ShowWindow(SW_HIDE);
	m_WndListEvent.ShowWindow(SW_SHOW);

	m_WndSearchOpt.SetOptEvent();

	m_nSearchType = search_event;
	m_WndMenuButton.SetBackupButtonStatus(m_WndListEvent.GetCheckStatus()||m_bPlayOn);
	m_WndMenuButton.SetDeleteButtonStatus(m_WndListEvent.GetCheckStatus());
	m_btnDelete.EnableWindow(m_WndListEvent.GetCheckStatus());
}

void CUIMain::OnShowGPSPanel()
{
	m_nWndGpsSize = m_WndGps.GetWndSize();
	CRect rect;
	this->GetWindowRect(&rect);
	ResizeServiceMenu(rect.Width(), rect.Height());
}


//////////////////////////////////////////////////////////////////////////
//
void CUIMain::PlayForward()
{
	if (m_bPlayOn == FALSE)
	{
		SetZoomDefault();
		m_pIntrPlayEx->SetSearchType(m_nSearchType);
	}

	m_bLoadUserData = TRUE;
	if (m_pIntrPlayEx->PlayForward())
	{
		if (m_bPlayOn == FALSE)
		{
			m_bPlayOn = TRUE;
			m_WndGps.SetPlayStatus(m_bPlayOn);
			m_WndTimeBar.SetPlayStatus(m_bPlayOn);
			m_WndMenuButton.SetBackupButtonStatus(m_bPlayOn);
			m_WndMenuButton.SetPrintButtonStatus(m_bPlayOn);
		}
		m_WndPlayer.SetPlayStatus(TRUE);
	}
}

void CUIMain::PlayBackward()
{
	if (m_bPlayOn == FALSE)
	{
		SetZoomDefault();
		m_pIntrPlayEx->SetSearchType(m_nSearchType);
	}

	m_bLoadUserData = TRUE;
	if (m_pIntrPlayEx->PlayBackward()&&(m_bPlayOn == FALSE))
	{
		m_bPlayOn = TRUE;
		m_WndGps.SetPlayStatus(m_bPlayOn);
		m_WndPlayer.SetPlayStatus(m_bPlayOn);
		m_WndTimeBar.SetPlayStatus(m_bPlayOn);
		m_WndMenuButton.SetBackupButtonStatus(m_bPlayOn);	//TRUE
		m_WndMenuButton.SetPrintButtonStatus(m_bPlayOn);

	}
}

void CUIMain::PlayStop()
{
	m_bLoadUserData = FALSE;
	Sleep(100);
	m_pIntrPlayEx->Stop();
	Sleep(100);
	m_pIntrPlayEx->ClearData();

	m_stImageInfo.stFileInfo.nCurIndex = -1;

	m_bPlayOn = FALSE;
	m_WndGps.SetPlayStatus(m_bPlayOn);
	m_WndPlayer.SetPlayStatus(m_bPlayOn);
	m_WndTimeBar.SetPlayStatus(m_bPlayOn);
	if (m_nSearchType == search_time)
		m_WndMenuButton.SetBackupButtonStatus(m_WndListTime.GetCheckStatus()||m_bPlayOn);
	else
		m_WndMenuButton.SetBackupButtonStatus(m_WndListEvent.GetCheckStatus()||m_bPlayOn);
	m_WndMenuButton.SetPrintButtonStatus(m_bPlayOn);

	ShowLogo();
}

void CUIMain::PlayStopReplay(LPST_NOTIFY_STOP lpstNotifyStop)
{
	m_bLoadUserData = FALSE;
	Sleep(100);
	m_pIntrPlayEx->Stop();
	Sleep(100);
	m_pIntrPlayEx->ClearData();

	if ((theApp.GetDemoMode() == 1)&&lpstNotifyStop->bContinue)
	{
		m_bLoadUserData = TRUE;
		int nList;
		m_WndGSensorGraph.ClearGSensor();

		switch (lpstNotifyStop->nPrePlayMode)
		{
		case pst_playforward:
			if (lpstNotifyStop->nSearchType == search_time)
				m_WndListTime.SetRecIndex(0);
			else
				m_WndListEvent.SetRecIndex(0);
			m_pIntrPlayEx->PlayForward();
			break;
		case pst_stepnext:
			if (lpstNotifyStop->nSearchType == search_time)
				m_WndListTime.SetRecIndex(0);
			else
				m_WndListEvent.SetRecIndex(0);
			m_pIntrPlayEx->StepForward(0);
			break;
		case pst_stepnextkeyframe:
			if (lpstNotifyStop->nSearchType == search_time)
				m_WndListTime.SetRecIndex(0);
			else
				m_WndListEvent.SetRecIndex(0);
			m_pIntrPlayEx->StepForwardKeyframe(0);
			break;
		case pst_playbackward:
			if (lpstNotifyStop->nSearchType ==  search_time)
			{
				nList = m_WndListTime.GetRecIndexSize();
				m_WndListTime.SetRecIndex(nList-1);
			}
			else
			{
				nList = m_WndListEvent.GetRecIndexSize();
				m_WndListEvent.SetRecIndex(nList-1);
			}
			m_pIntrPlayEx->PlayBackward();
			break;
		case pst_stepprevkeyframe:
			if (lpstNotifyStop->nSearchType ==  search_time)
			{
				nList = m_WndListTime.GetRecIndexSize();
				m_WndListTime.SetRecIndex(nList-1);
			}
			else
			{
				nList = m_WndListEvent.GetRecIndexSize();
				m_WndListEvent.SetRecIndex(nList-1);
			}
			m_pIntrPlayEx->StepBackwardKeyframe(nList-1);
			break;

		}

		m_bPlayOn = TRUE;
		m_WndGps.SetPlayStatus(m_bPlayOn);
		m_WndPlayer.SetPlayStatus(m_bPlayOn);
		m_WndTimeBar.SetPlayStatus(m_bPlayOn);
		m_WndMenuButton.SetBackupButtonStatus(m_bPlayOn);
		m_WndMenuButton.SetPrintButtonStatus(m_bPlayOn);

		lpstNotifyStop->bContinue = TRUE;
	}
	else
	{
		m_stImageInfo.stFileInfo.nCurIndex = -1;

		m_bPlayOn = FALSE;
		m_WndGps.SetPlayStatus(m_bPlayOn);
		m_WndPlayer.SetPlayStatus(m_bPlayOn);
		m_WndTimeBar.SetPlayStatus(m_bPlayOn);
		if (m_nSearchType == search_time)
			m_WndMenuButton.SetBackupButtonStatus(m_WndListTime.GetCheckStatus()||m_bPlayOn);
		else
			m_WndMenuButton.SetBackupButtonStatus(m_WndListEvent.GetCheckStatus()||m_bPlayOn);
		m_WndMenuButton.SetPrintButtonStatus(m_bPlayOn);

		ShowLogo();
		lpstNotifyStop->bContinue = FALSE;
	}
}


void CUIMain::Pause()
{
	if (m_pIntrPlayEx->GetPlayerState() != pst_stopped)
	{
		int nPlayerState = m_pIntrPlayEx->GetPlayerState();
		if (nPlayerState != pst_pause)
			m_nPlayerState = nPlayerState;
		m_WndPlayer.SetPlayStatus(FALSE);

		m_pIntrPlayEx->Pause();
	}
}

void CUIMain::StepForward()
{
	m_pIntrPlayEx->StepForward();
	m_WndPlayer.SetPlayStatus(FALSE);
}

void CUIMain::StepForwardKeyframe()
{
	m_pIntrPlayEx->StepForwardKeyframe();
	m_WndPlayer.SetPlayStatus(FALSE);
}

void CUIMain::StepBackward()
{
	//does not support
}

void CUIMain::StepBackwardKeyframe()
{
	m_pIntrPlayEx->StepBackwardKeyframe();
	m_WndPlayer.SetPlayStatus(FALSE);
}

void CUIMain::NextFile()
{
	if (m_bMoveFile)
		return;
	m_bMoveFile = TRUE;

	//1. pause
	int nSearchType;
	int nCurrentPlayList;
	int nNext;
	int nPlayerState;
	BOOL bDirection = TRUE;

	if (!m_bFileOpen)
	{
		AfxMessageBox(m_sarrMessage.GetAt(Message_10_0));
		m_bMoveFile = FALSE;
		return;
	}

	if (m_bPlayOn)
	{
		nPlayerState = m_pIntrPlayEx->GetPlayerState();
		if (nPlayerState == pst_pause)
			nPlayerState = m_nPlayerState;

		switch (nPlayerState)
		{
		case pst_playbackward:
		case pst_stepprev:
		case pst_stepprevkeyframe:
			bDirection = FALSE;
			break;
		}
		m_pIntrPlayEx->Pause();
	}

	//2. find next file
	if (m_bPlayOn)
	{
		nSearchType = m_pIntrPlayEx->GetSearchType();
	}
	else
	{
		nSearchType = m_nSearchType;
	}

	if (nSearchType == search_time)
	{
		nCurrentPlayList = m_WndListTime.GetCurrentRecIndexNumber();
		nNext = m_WndListTime.GetRecIndexNumberAvailable(nCurrentPlayList+1);
		if (nNext>=0)
			m_WndListTime.SetRecIndex(nNext);
	}
	else
	{
		nCurrentPlayList = m_WndListEvent.GetCurrentRecIndexNumber();
		nNext = m_WndListEvent.GetRecIndexNumberAvailable(nCurrentPlayList+1);
		if (nNext>=0)
			m_WndListEvent.SetRecIndex(nNext);
	}
	//3. if no next file continue to play 
	if (nNext<0)
	{
		if (m_bPlayOn)
		{
			m_pIntrPlayEx->PlayForward();
			m_WndPlayer.SetPlayStatus(m_bPlayOn);
		}
		AfxMessageBox(m_sarrMessage.GetAt(Message_10_1));
		m_bMoveFile = FALSE;
		return;
	}

	//4. otherwise stop playing current file and start playing next file
	if (m_bPlayOn)
	{
		m_bLoadUserData = FALSE;
		Sleep(100);
		m_pIntrPlayEx->Stop();
		Sleep(100);
		m_pIntrPlayEx->ClearData();
		m_bLoadUserData = TRUE;
		m_pIntrPlayEx->PlayList(nNext, bDirection);
		m_WndPlayer.SetPlayStatus(m_bPlayOn);
	}
	m_bMoveFile = FALSE;
}

void CUIMain::PrevFile()
{
	if (m_bMoveFile)
		return;
	TRACE(_T("PrevFile - start\n"));
	m_bMoveFile = TRUE;

	//1. pause
	int nSearchType;
	int nCurrentPlayList;
	int nPrev;
	int nPlayerState;
	BOOL bDirection = TRUE;

	if (!m_bFileOpen)
	{
		AfxMessageBox(m_sarrMessage.GetAt(Message_10_0));
		m_bMoveFile = FALSE;
		return;
	}

	if (m_bPlayOn)
	{
		nPlayerState = m_pIntrPlayEx->GetPlayerState();
		if (nPlayerState == pst_pause)
			nPlayerState = m_nPlayerState;

		switch (nPlayerState)
		{
		case pst_playbackward:
		case pst_stepprev:
		case pst_stepprevkeyframe:
			bDirection = FALSE;
			break;
		}
		m_pIntrPlayEx->Pause();
	}

	//2. find next file
	if (m_bPlayOn)
	{
		nSearchType = m_pIntrPlayEx->GetSearchType();
	}
	else
	{
		nSearchType = m_nSearchType;
	}

	if (nSearchType == search_time)
	{
		nCurrentPlayList = m_WndListTime.GetCurrentRecIndexNumber();
		nPrev = m_WndListTime.GetRecIndexNumberAvailable(nCurrentPlayList-1);
		if (nPrev>=0)
			m_WndListTime.SetRecIndex(nPrev);
	}
	else
	{
		nCurrentPlayList = m_WndListEvent.GetCurrentRecIndexNumber();
		nPrev = m_WndListEvent.GetRecIndexNumberAvailable(nCurrentPlayList-1);
		if (nPrev>=0)
			m_WndListEvent.SetRecIndex(nPrev);
	}

	//3. if no next file continue to play 
	if (nPrev<0)
	{
		if (m_bPlayOn)
		{
			m_pIntrPlayEx->PlayForward();
			m_WndPlayer.SetPlayStatus(m_bPlayOn);
		}
		AfxMessageBox(m_sarrMessage.GetAt(Message_10_2));
		m_bMoveFile = FALSE;
		return;
	}

	//4. otherwise stop playing current file and start playing next file
	if (m_bPlayOn)
	{
		m_bLoadUserData = FALSE;
		Sleep(100);
		m_pIntrPlayEx->Stop();
		Sleep(100);
		m_pIntrPlayEx->ClearData();
		m_bLoadUserData = TRUE;
		m_pIntrPlayEx->PlayList(nPrev, bDirection);
		m_WndPlayer.SetPlayStatus(m_bPlayOn);
	}
	m_bMoveFile = FALSE;
	TRACE(_T("PrevFile - end\n"));
}

void CUIMain::PlayNPause()
{
	if (!m_bPlayOn)
	{
		PlayForward();
	}
	else
	{
		int nState;
		nState = m_pIntrPlayEx->GetPlayerState();
		if (nState == pst_pause)
		{
			switch (m_nPlayerState)
			{
			case pst_playforward:
			case pst_stepnext:
			case pst_stepnextkeyframe:
			case pst_stepprev:
			case pst_stepprevkeyframe:
				PlayForward();
				break;
			case pst_playbackward:
				PlayBackward();
				break;
			}
		}
		else
		{
			switch (nState)
			{
			case pst_playforward:
			case pst_playbackward:
				Pause();
				break;
			case pst_stepnext:
			case pst_stepnextkeyframe:
			case pst_stepprev:
			case pst_stepprevkeyframe:
				PlayForward();
				break;
			}
		}
	}
}

void CUIMain::StopNPlay()
{
	if (m_bPlayOn)
	{
		int nStatus;
		nStatus = m_pIntrPlayEx->GetPlayerState();
		if (nStatus == pst_pause)
			nStatus = m_nPlayerState;

		PlayStop();
		switch (nStatus)
		{
		case pst_playforward:
		case pst_stepnext:
		case pst_stepnextkeyframe:
		case pst_stepprev:
		case pst_stepprevkeyframe:
			PlayForward();
			break;
		case pst_playbackward:
			PlayBackward();
			break;
		}
	}
}

void CUIMain::SetVolume(int nVolume)
{
	CUtilSound  utilSound;				

	if (utilSound.InitMasterVolume())
	{
		DWORD dwVol = utilSound.GetMasterVolume();
		dwVol = nVolume*1092;
		utilSound.SetMasterVolume(dwVol);

		m_dwVolume = nVolume;
	}
}

void CUIMain::SearchEvent(DWORD dwRecType)
{
	BOOL bRet;
	CallbackReplay(EVENTLIST_OPTION, dwRecType, &bRet, this);

	m_WndListTime.SetSelectRecType(dwRecType, bRet);
	m_WndListTime.ArrangeRecList();
	if (dwRecType != REC_NORMAL)
	{
		m_WndListEvent.SetSelectRecType(dwRecType, bRet);
		m_WndListEvent.ArrangeRecList();
	}
	PlayStop();
}


//////////////////////////////////////////////////////////////////////////
//DXSMessage
void CUIMain::SetZoom(LPST_WHEEL_INFO lpstWheelInfo)
{
	if (!m_bPlayOn)
		return;

	int nZoom;
	int nID = lpstWheelInfo->nID;
	nZoom = m_pWndDisplay[nID]->GetZoomInfo();
	if (lpstWheelInfo->zDelta<0)
	{
		nZoom -= ZOOM_UNIT;
	}
	else if (lpstWheelInfo->zDelta>0)
	{
		nZoom += ZOOM_UNIT;
	}
	m_pWndDisplay[nID]->SetZoomInfo(nZoom, lpstWheelInfo->pt);
}

//////////////////////////////////////////////////////////////////////////
void CUIMain::SetZoomDefault()
{
	int i;
	for (i=0;i<MAX_CHANNEL;i++)
		m_pWndDisplay[i]->SetZoomInfo(ZOOM_DEFAULT, CPoint(-1, -1));
	m_ExtScreen3.SetZoomInfo(ZOOM_DEFAULT, CPoint(-1, -1));
}

void CUIMain::SetZoomPIP()
{
	int i;
	VIEWER_SETTING stViewerSet;
	stViewerSet = m_dlgViewerSetup.GetViewerSet();

	for (i=0;i<MAX_CHANNEL;i++)
	{
		m_pWndDisplay[i]->SetDispZoomPIP(stViewerSet.bShowZoomPip, stViewerSet.bZoomPipPos);
		m_pWndDisplay[i]->SetZoomOption(stViewerSet.bUseCtrlKey);
	}
	m_ExtScreen3.SetDispZoomPIP(stViewerSet.bShowZoomPip, stViewerSet.bZoomPipPos);
	m_ExtScreen3.SetZoomOption(stViewerSet.bUseCtrlKey);
	
}

//////////////////////////////////////////////////////////////////////////
